﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using DWEIP.Web.Core;
using DWEIP.Models.Sys;
using DWEIP.Models.tam;
using DWEIP.Models.tsm;
using DWEIP.Models.OA;
using DWEIP.BLL;
using DWEIP.BLL.Sys;
using DWEIP.BLL.OA;
using DWEIP.Common;
using DataWin.Core.Util;

namespace DWEIP.Web.Controllers
{
    public class HomeController : BaseController
    {
        #region UI框架
        private SysConfigModel siteConfig = new SysConfigBLL().loadConfig(Utils.GetXmlMapPath("Configpath"));

        public ActionResult Index()
        {
            if (Session["Account"] != null)
            {
                //获取是否开启WEBIM
                ViewBag.IsEnable = siteConfig.webimstatus;
                //获取信息间隔时间
                ViewBag.NewMesTime = siteConfig.refreshnewmessage;
                //系统名称
                ViewBag.WebName = siteConfig.webname;
                //公司名称
                ViewBag.ComName = siteConfig.webcompany;
                //版权
                ViewBag.CopyRight = siteConfig.webcopyright;
                //在线人数
                //OnlineUserRecorder recorder = HttpContext.Cache[OnlineHttpModule.g_onlineUserRecorderCacheKey] as OnlineUserRecorder;
                ViewBag.OnlineCount = 100;// recorder.GetUserList().Count;
                tam05Model tam05 = new tam05Model();
                tam05 = (tam05Model)Session["Account"];
                return View(tam05);
            }
            else
            {
                return RedirectToAction("Index", "Account");
            }
        }

        //父ID=0的数据为顶级菜单
        public JsonResult GetTopMenu()
        {
            //加入本地化
            string infoName = Utils.GetLanguageInfo();
            if (Session["Account"] != null)
            {
                tam05Model tam05 = (tam05Model)Session["Account"];
                List<SysModuleModel> list = new HomeBLL().GetTopMenuByPersonId(tam05.ae01);
                //List<tsm02Model> list = new HomeBLL().GetMenuByPersonId(tam05.ae01, "0");
                var json = from r in list
                           select new SysModuleNavModel()
                           {
                               id = r.Id,
                               text = infoName.IndexOf("zh") > -1 || infoName == "" ? r.Text : r.Text,
                               attributes = (infoName.IndexOf("zh") > -1 || infoName == "" ? "zh-CN" : "en-US") + "/" + r.Url,
                               iconCls = r.Iconic
                           };

                return Json(json);

            }

            return Json("0", JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取导航菜单
        /// </summary>
        /// <param name="id">所属</param>
        /// <returns>树</returns>
        public JsonResult GetTreeByEasyui(string id)
        {
            if (id=="0")
            {
                id = "";
            }
            //加入本地化
            //CultureInfo info = Thread.CurrentThread.CurrentCulture;
            string infoName = Utils.GetLanguageInfo();
            if (Session["Account"] != null)
            {
                //加入本地化
                tam05Model tam05 = (tam05Model)Session["Account"];
                List<SysModuleModel> list = new HomeBLL().GetTreeByPersonId(tam05.ae01, id);
                var json = from r in list
                           select new SysModuleNavModel()
                           {
                               id = r.Id,
                               text = infoName.IndexOf("zh") > -1 || infoName == "" ? r.Text : r.Text,
                               attributes = (infoName.IndexOf("zh") > -1 || infoName == "" ? "zh-CN" : "en-US") + "/" + r.Url??"",
                               iconCls = r.Iconic,
                               //state = (m_BLL.GetList(r.Id).Count > 0) ? "closed" : "open"
                               //state = "closed"
                               state = ((r.Url??"").Length == 0) ? "closed" : "open"
                           };
                return Json(json);
            }
            else
            {
                return Json("0", JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult SetThemes(string theme, string menu, bool topmenu)
        {

            return Json("1", JsonRequestBehavior.AllowGet);
        }

        public ActionResult TopInfo()
        {
            if (Session["Account"] != null)
            {
                tam05Model tam05 = new tam05Model();
                tam05 = (tam05Model)Session["Account"];
                return View(tam05);
            }
            return View();
        }
        #endregion

        #region js配置

        public JavaScriptResult ConfigJs()
        {
            //CultureInfo info = Thread.CurrentThread.CurrentCulture;
            StringBuilder sb = new StringBuilder();
            sb.Append("var _DWGlobal;");
            sb.Append("(function(_DWGlobal) {");
            sb.Append("    var Config = (function() {");
            sb.Append("        function Config() {}");
            sb.AppendFormat("  Config.currentCulture = '{0}';", Utils.GetLanguageInfo());
            sb.AppendFormat("  Config.apiUrl = '{0}';", "");
            sb.AppendFormat("  Config.token = '{0}';", "");
            sb.Append("       return Config;");
            sb.Append("   })();");
            sb.Append("   _DWGlobal.Config = Config;");
            sb.Append(" })(_DWGlobal || (_DWGlobal = { }));");

            return JavaScript(sb.ToString());
        }

        #endregion

        public ActionResult Desktop()
        {
            WebpartBLL webPartBLL = new WebpartBLL();
            SysUserConfig ss = webPartBLL.GetByIdAndUserId("webpart", GetUserId());
            if (ss != null)
            {
                ViewBag.Value = ss.Value;
            }
            else
            {
                ViewBag.Value = "";
            }
            return View();
        }

        [HttpPost]
        public JsonResult GetPartDataByNotice()
        {
            List<nblt001Model> list = new List<nblt001Model>();
            GridPager pager = new GridPager()
            {
                page = 1,
                rows = 8,
                sort = "Id",
                order = "desc"
            };

            if (Session["Account"] != null)
            {
                tam05Model tam05 = new tam05Model();
                tam05 = (tam05Model)Session["Account"];

                OA_BltBLL blt_BLL = new OA_BltBLL();
                list = blt_BLL.GetList(ref pager, "", "", true, tam05.ae01, 2);
            }

            StringBuilder sb = new StringBuilder("");
            sb.Append("<table style=\"width:100%\">");
            foreach (var blt_i in list)
            {
                sb.AppendFormat("<tr style='height:33px;line-height:33px; padding:5px;'><td>&nbsp;&nbsp;&nbsp;[{0}]&nbsp;<a class=\"color-black\" href=\"javascript:ShowInfo('{1}','{2}')\">{3}</a></td><td class=\"color-black\" style=\"width:75px\">[{4}]</td></tr>", blt_i.folder_title, blt_i.title, blt_i.pk_id, blt_i.title, blt_i.publish_date);
            }
            sb.Append("</table>");
            return Json(sb.ToString(), JsonRequestBehavior.AllowGet);

        }
    }
}